package com.android.ContactPresets;
/**
 * @author Sridhar Jagannathan (sxjthefirst@gmail.com)
 */
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ContactPresets extends ListActivity {

	ContactsDbAdapter cdb;

	private static final int INSERT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1;
	private static final int EDIT_ID = Menu.FIRST + 2;
	private static final int VIEW_ID = Menu.FIRST + 3;
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;
	private static final int ACTIVITY_VIEW = 2;

	private AutoCompleteTextView mSearch;
	private SimpleCursorAdapter mContacts;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.contacts_list);
		cdb = new ContactsDbAdapter(this);
		cdb.open();
		
		//Get and add functionality to the Search autocomplete view
		mSearch=(AutoCompleteTextView)findViewById(R.id.search);
		mSearch.addTextChangedListener(new TextWatcher(){
			public void beforeTextChanged(CharSequence text, int i, int j, int k)
			{
				//Do nothing 
			}
			public void onTextChanged(CharSequence text, int i, int j, int k)
			{
				String query = ContactsDbAdapter.KEY_NAME +" like " + "\"" + text + "%\"";
				Cursor cursor=cdb.runQuery(query);
				if (cursor.count()!=0)
					mContacts.changeCursor(cursor);
			}

		});

		fillData();

	}

	/**
	 * Reads the contacts database and fills the displayed contacts list.
	 */
	private void fillData() {
		Cursor contactsCursor = cdb.fetchAllContacts();

		startManagingCursor(contactsCursor);

		// Create an array to specify the fields we want to display in the list
		String[] from = new String[] { ContactsDbAdapter.KEY_NAME };
		// and an array of the fields we want to bind those fields to
		int[] to = new int[] { R.id.text1 };
		// Now create a simple cursor adapter and set it to display
		mContacts= new SimpleCursorAdapter(this,
				R.layout.contacts_row, contactsCursor, from, to);
		setListAdapter(mContacts);

	
		// Populate the names to the AutoComplete TextView 
		String[] al1=new String[contactsCursor.count()] ;
		contactsCursor.first();
		for (int i=0; i<contactsCursor.count(); i++)
		{
			al1[i]= contactsCursor.getString(1);
			contactsCursor.next();
		}

		ArrayAdapter<String> arrayadapter= new ArrayAdapter<String>(this, R.layout.contacts_row,al1);
		mSearch.setAdapter(arrayadapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, R.string.menu_insert);
		menu.add(0, DELETE_ID, R.string.menu_delete);
		menu.add(0, EDIT_ID, R.string.menu_edit);
		menu.add(0, VIEW_ID, R.string.menu_view);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, Item item) {
		long id;
		switch (item.getId()) {
		case INSERT_ID:
			Intent i1 = new Intent(this, ContactsEdit.class);
			startSubActivity(i1, ACTIVITY_CREATE);
			return true;
		case EDIT_ID:
			id = getListView().getSelectedItemId();
			if (id>0)
			{
				Intent i2 = new Intent(this, ContactsEdit.class);
				i2.putExtra(ContactsDbAdapter.KEY_ROWID, id);
				startSubActivity(i2, ACTIVITY_EDIT);
			}
			return true;
		case DELETE_ID:
			// Show a confirmation dialog before deleting
			new AlertDialog.Builder(this)
			.setIcon(R.drawable.contactsicon)
			.setTitle(getResources().getString(R.string.menu_delete))
			.setMessage(R.string.delete_msg)
			.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() 
			{ 
				// If Confirm is clicked
				public void	onClick(DialogInterface dialog, int arg1) {
					long id = getListView().getSelectedItemId();
					if (id > 0)
					{
						cdb.deleteContact(id);
						fillData();
					}

				} 
			})
			.setNegativeButton(R.string.cancel, new	DialogInterface.OnClickListener()
			{ 
				//If Cancel is clicked
				public void onClick(DialogInterface dialog, int arg1) 
				{ 
					// do nothing 
				} 
			})
			.show(); //Display the dialog
			return true;
		case VIEW_ID:
			id = getListView().getSelectedItemId();
			if (id>0)
			{
				Intent i2 = new Intent(this, ContactsView.class);
				i2.putExtra(ContactsDbAdapter.KEY_ROWID, id);
				startSubActivity(i2, ACTIVITY_VIEW);
			}
			return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent i = new Intent(this, ContactsView.class);
		i.putExtra(ContactsDbAdapter.KEY_ROWID, id);
		startSubActivity(i, ACTIVITY_VIEW);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			String data, Bundle extras) {
		if (resultCode == RESULT_OK )
		{
			super.onActivityResult(requestCode, resultCode, data, extras);
			fillData();
		}
	}
}